package com.example.drug.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.example.common.entity.RxMain;
import com.example.drug.dto.req.XRxListReq;
import com.example.drug.dto.resp.DiagnosisListRsp;
import com.example.drug.jpa.RxMainRepository;
import com.example.drug.mapper.PatientMapper;
import com.example.drug.service.PatientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sifan
 * @PackagName:com.example.doctor.service.impl
 * @ClassName: PatientServiceImpl
 * @Description:
 * @date:2021/1/19 19:14
 */
@Service
@Slf4j
public class PatientServiceImpl implements PatientService {
    @Resource
    PatientMapper patientMapper;

    @Resource
    RxMainRepository rxMainRepository;
    @Override
    public PageInfo<DiagnosisListRsp> DiagnosisList(XRxListReq xRxListReq) {
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -6);
        PageInfo<DiagnosisListRsp> objectPageInfo = PageHelper.startPage(xRxListReq.getPage(), xRxListReq.getLimit()).doSelectPageInfo(() -> patientMapper.queryDiagnosisListByPid(xRxListReq.getPID(), dateTime));
        List<DiagnosisListRsp> diagnosisListRspList = objectPageInfo.getList();
        for (DiagnosisListRsp diagnosisListRsp : diagnosisListRspList) {
            List<RxMain> rxMainList = rxMainRepository.findByOrderNOAndIsTrialPharmCa(diagnosisListRsp.getOrderNO(),true);
            diagnosisListRsp.setDisease(String.join("、", rxMainList.stream().map(RxMain::getDialectical).collect(Collectors.toList())));
        }
        return objectPageInfo;
    }


}
